package com.kedacom.freedomLabel.model.bean

import android.arch.persistence.room.Entity
import android.arch.persistence.room.PrimaryKey
import android.util.Base64
import com.google.zxing.qrcode.encoder.QRCode
import com.kedacom.freedomLabel.model.db.dao.ConfigDao
import com.kedacom.freedomLabel.utils.log.LLog

@Entity
class InputRecord(
        @PrimaryKey
        val id: String,
        val sessid: String,
        val username: String,
        val store: String,
        val storeName: String,
        val kind: String,
        val kindName: String,
        val level: String,
        val levelName: String,
        val number: Float,
        val price: Float,
        /**
         * 行政区code
         */
        val originCode: String,
        /**
         * 行政区名称
         */
        val originName: String,
        /**
         * 详细地址
         */
        val origin: String,
        val date: String,
        val mac: String,
        val order: String?, // 上传成功批次号
        val codeURL: String?,
        val uploaded: Boolean, // 已上传
        val printed: Boolean  // 已打印
) {

    /**
     * 获取二维码编码
     */
    var qRCodeString: String = ""
        get() {
            val codeString = "$order&$kindName&$levelName&$storeName"
            LLog.debug(codeString)
            return Base64.encodeToString(codeString.toByteArray(), Base64.NO_WRAP)
        }
}

@Entity
class OutputRecord(
        @PrimaryKey
        val id: String,
        val username: String,
        val sessid: String,
        val entryOrder: String, // 入库批次号
        val storeName: String,
        val kindName: String,
        var levelName: String,
        val number: Float,
        val price: Float,
        val addressCode: String, // 行政区code
        val addressName: String, // 行政区名称
        val address: String, // 详细地址
        val date: String,
        val mac: String?,
        val order: String?, // 出库批次号
        val codeURL: String?
)


class PrintRecord(val isInput: Boolean,
                  val orderNumber: String?, val productName: String,
                  var levelName: String, val storeName: String,
                  val number: String, val date: String,
                  val qrCode: String?)
class PrintRecordBusiness(val serial_number: Int,
                  val business: Business,
                  var template: Template, val direction: String,
                  val columnNames: Array<String>, val booleansNames:Array<Boolean>,
                  val dao: ConfigDao?)


class PrintByIdName(var template: Template,
              val titleName: String,
                          val weight: String,
                          val weightUnit: String,
                          val scanzxing: String,
                          val dateStr: Long
                         )

class RecentRecord(
        val displayName: String,
        val disPlayWeight: String,
        val displayOrder: String,
        val displayDateTime: String,
        val dateTime: String,
        val displayPrice: String,
        val displayStorageRoom: String,
        val displayArea: String,
        val displayAddress: String,
        val isInputRecord: Boolean,
        val inputRecord: InputRecord? = null,
        val outputRecord: OutputRecord? = null
) {

    constructor(inputRecord: InputRecord) : this(
            "${inputRecord.levelName} ${inputRecord.kindName}",
            "${inputRecord.number}",
            "${inputRecord.order}",
            inputRecord.date.subSequence(5, 19).toString(),
            inputRecord.date,
            "${inputRecord.price} 元",
            inputRecord.storeName,
            inputRecord.originName,
            inputRecord.origin,
            true,
            inputRecord,
            null
    )

    constructor(outputRecord: OutputRecord) : this(
            "${outputRecord.levelName} ${outputRecord.kindName}",
            "${outputRecord.number}",
            "${outputRecord.order}",
            outputRecord.date.subSequence(5, 19).toString(),
            outputRecord.date,
            "${outputRecord.price} 元",
            outputRecord.storeName,
            outputRecord.addressName,
            outputRecord.address,
            false,
            null,
            outputRecord
    )

}